package jmars.mobile.misc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Java利用CellID LAC调用Google接口获取经纬度例子
 */
public class GoogleJson {
	public static void main(String args[]) {
		RqsLocation("39755","59203");
	}


	private static String getJson(String lac,String cid) {
		String json = "{"
				+ "\"homeMobileCountryCode\": 460,"
				+ "\"homeMobileNetworkCode\": 1,"
				+ "\"radioType\": \"gsm\","
				+ "\"carrier\": \"China Union\","
				+ "\"cellTowers\": [{"
				+ "\"cellId\":"+cid+","
				+ "\"locationAreaCode\": "+lac+","
				+ "\"mobileCountryCode\": 460,"
				+ "\"mobileNetworkCode\": 1,"
				+ "\"age\": 0,"
				+ "\"signalStrength\": -60,"
				+ "\"timingAdvance\": 15}"
				+ "],\"wifiAccessPoints\": []}";
		return json;
//		return "{\"homeMobileCountryCode\": 310,\"homeMobileNetworkCode\": 260, \"radioType\": \"gsm\", \"carrier\": \"T-Mobile\", cellTowers\": [{\"cellId\": 39627456,   \"locationAreaCode\": 40495,  \"mobileCountryCode\": 310,\"mobileNetworkCode\": 260,\"age\": 0,   \"signalStrength\": -95}],\"wifiAccessPoints\": [{\"macAddress\": \"01:23:45:67:89:AB\",\"signalStrength\": 8,\"age\": 0,\"signalToNoiseRatio\": -65,\"channel\": 8},{\"macAddress\": \"01:23:45:67:89:AC\",\"signalStrength\": 4,\"age\": 0}]}";
	}
	
	
	
	
	private static Boolean RqsLocation(String lac, String cid){
		   
		   Boolean result = false;
		   HttpURLConnection conn = null;
		   String urlmmap = "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyBHLtpSogAUpzckKzZduHXSWy4L235y_qU";
		 
		      try {
		       URL url = new URL(urlmmap);
		       conn = (HttpURLConnection) url.openConnection();
	            conn.setDoOutput(true);
	            conn.setRequestMethod("POST");

	            String json = getJson(lac,cid);
	            System.out.println(json);

	            conn.getOutputStream().write(json.getBytes());
	            conn.getOutputStream().flush();
	            conn.getOutputStream().close();
	            int code = conn.getResponseCode();
	            System.out.println("code " + code);
	            BufferedReader in = new BufferedReader(new InputStreamReader(
	                    conn.getInputStream()));
	            String inputLine;
	            inputLine = in.readLine();
	            System.out.println(inputLine);
	            in.close();
	            // 解析结果
	            // JSONObject result = new JSONObject(inputLine);
	            // JSONObject location = result.getJSONObject("location");
	            // JSONObject address = location.getJSONObject("address");

	            // System.out.println("city = " + address.getString("city"));
	            // System.out.println("region = " + address.getString("region"));
		 } catch (IOException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
		 }
		 
		 return result;
		   
		  }

}
